package exhi.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.util.Identities;
import com.stp.core.util.WebUtil;
import com.stp.survey.entity.SurveyAnswer;
import com.stp.survey.entity.SurveyInfo;
import com.stp.survey.entity.SurveyQuest;
import com.stp.survey.manager.SurveyAnswerMng;
import com.stp.survey.manager.SurveyMng;

import exhi.entity.EntpBexhi;
import exhi.entity.EntpUser;
import exhi.manager.EntpBexhiMng;

@Controller
@RequestMapping("/gov")
public class SurveyExhiAct extends BaseAct {

	private static final String PATH_SURVEY_TABLE = "/exhi-survey-table";
	private static final String PATH_SURVEY_LIST = "/exhi-survey-list";
	private static final String PATH_SURVEY_DETAIL = "/exhi-survey-detail";
	private static final String PATH_SURVEY_SAVE = "/exhi-survey-save";
	private static final String PATH_SURVEY_VIEW = "/exhi-survey-view";

	private static final String PATH_SURVEY_EVAL = "/eval-survey-view";
	private static final String PATH_EVAL = "/eval-view";
	private static final String PATH_EXHI_EVAL = "/eval-exhi-view";

	private String code = "";
	private String surveyId = "";

	@RequestMapping(PATH_SURVEY_TABLE)
	public void table(@RequestParam(value = "code") String code, ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("colName", getDeptTypeName(code));
	}

	// 权限json数据
	@RequestMapping(PATH_SURVEY_LIST)
	@ResponseBody
	public String infoList(XGridPage page, @RequestParam("code") String code,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {
		this.code = code;
		surveyId = getSurveyId(code);
		String sql = "from EntpBexhi info where 1=1 and info.status in ('60','90')";
		sql += sqlModel.getSQLChars("info.zhlx", "=", capCode, false, "and");
		sql += sqlModel
				.getSQLChars("info.zlhmc", "like", capName, false, "and");
		sql += " order by info.createtime desc";
		ExhiGridRow row = new SurveyExhiAct.ExhiGridRow();
		XGrid grid = entpBexhiMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}

	class ExhiGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			String surveyId = "";
			if (obj instanceof EntpBexhi) {
				xrow = new XGridRow();
				EntpBexhi info = (EntpBexhi) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add(info.getZlhmc());
				surveyId = SurveyExhiAct.this.surveyId;
				t.add("<a href='exhi-survey-detail.jspx?sid=" + surveyId
						+ "&type=" + SurveyExhiAct.this.code + "&infoId="
						+ info.getId() + "'>【评估】</a>");
				xrow.setData(t);
			}
			return xrow;
		}
	}

	// 详细信息
	@RequestMapping(PATH_SURVEY_DETAIL)
	public void surveyDetail(@RequestParam("type") String code,
			@ModelAttribute("surveyInfo") SurveyInfo surveyInfo,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("colName", surveyInfo.getSurveyName());
		model.addAttribute("sid", surveyInfo.getId());
		if (null == surveyInfo.getId()
				|| "".equalsIgnoreCase(surveyInfo.getId())) {
			model.addAttribute("surveyId", Identities.uuid());
		} else {
			model.addAttribute("surveyId", surveyInfo.getId());
		}
		model.addAttribute("code", code);
		model.addAttribute("survey", surveyInfo);
		model.addAttribute("info", entpBexhiInfo);
		model.addAttribute("quests",
				surveyMng.findQuest("surveyId", surveyInfo.getId(), "sort"));
		String sql = "from SurveyAnswer info where surveyId='"
				+ surveyInfo.getId() + "'";
		String uid = "";
		if (null != getEntpUser())
			uid = getEntpUser().getUid();
		else
			uid = getUser("userRbacEntp").getId();
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='" + code + "'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		showView(entpBexhiInfo,model);
		model.addAttribute("answers", surveyAnswerMng.list(sql));
	}

	@RequestMapping(PATH_SURVEY_SAVE)
	public String surveySave(@RequestParam("code") String code,
			@ModelAttribute("surveyInfo") SurveyInfo surveyInfo,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo,
			HttpServletRequest request, ModelMap model) {
		List<SurveyQuest> quests = surveyMng.findQuest("surveyId",
				surveyInfo.getId(), "sort");
		String val;
		SurveyAnswer answer;
		String uid = "";
		if (null != getEntpUser())
			uid = getEntpUser().getUid();
		else
			uid = getUser("userRbacEntp").getId();
		String sql = "delete from std_quest2answer where answer_user='" + uid
				+ "'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		sql += " and p0='" + code + "'";
		surveyAnswerMng.query(sql);
		for (SurveyQuest q : quests) {
			answer = new SurveyAnswer();
			if ("1".equalsIgnoreCase(q.getQuestType())) {
				val = request.getParameter(q.getId());
				answer.setQuestId(q.getId());
				answer.setSurveyId(surveyInfo.getId());
				answer.setSimpleAnswer(val);
			}
			if ("2".equalsIgnoreCase(q.getQuestType())) {
				val = request.getParameter(q.getId());
				answer.setQuestId(q.getId());
				answer.setSurveyId(surveyInfo.getId());
				answer.setSimpleAnswer(val);
			}
			if ("3".equalsIgnoreCase(q.getQuestType())) {
				val = request.getParameter(q.getId());
				answer.setQuestId(q.getId());
				answer.setSurveyId(surveyInfo.getId());
				answer.setSimpleAnswer(val);
			}
			answer.setAnswerTime(new Date());
			answer.setAnswerIp(WebUtil.getIpAddr(request));
			answer.setAnswerUser(uid);
			answer.setP0(code);
			answer.setP1(entpBexhiInfo.getId());
			surveyAnswerMng.save(answer);
		}
		return "redirect:" + PATH_SURVEY_VIEW.substring(1) + ".jspx?infoId="
				+ entpBexhiInfo.getId() + "&sid=" + surveyInfo.getId()
				+ "&code=" + code;
	}

	@RequestMapping(PATH_SURVEY_VIEW)
	public void surveyView(@RequestParam("code") String code,
			@ModelAttribute("surveyInfo") SurveyInfo surveyInfo,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		model.addAttribute("survey", surveyInfo);
		model.addAttribute("info", entpBexhiInfo);
		model.addAttribute("quests",
				surveyMng.findQuest("surveyId", surveyInfo.getId(), "sort"));
		String sql = "from SurveyAnswer info where surveyId='"
				+ surveyInfo.getId() + "'";
		String uid = "";
		if (null != getEntpUser())
			uid = getEntpUser().getUid();
		else
			uid = getUser("userRbacEntp").getId();
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='" + code + "'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		showView(entpBexhiInfo,model);
		model.addAttribute("answers", surveyAnswerMng.list(sql));
	}

	@RequestMapping(PATH_SURVEY_EVAL)
	public void surveyEval(@RequestParam("code") String code,
			@ModelAttribute("surveyInfo") SurveyInfo surveyInfo,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		surveyView(code, surveyInfo, entpBexhiInfo, model);
	}

	@RequestMapping(PATH_EVAL)
	public void evalView(@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo,
			ModelMap model) {
		model.addAttribute("info", entpBexhiInfo);
		String uid = "";
		String sql = "";
		if (null != getEntpUser())
			uid = getEntpUser().getUid();
		else
			uid = getUser("userRbacEntp").getId();
		//
		surveyId = getSurveyId("PL");
		model.addAttribute("q0",
				surveyMng.findQuest("surveyId", surveyId, "sort"));
		sql = "from SurveyAnswer info where surveyId='" + surveyId + "'";
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='PL'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		model.addAttribute("a0", surveyAnswerMng.list(sql));

		surveyId = getSurveyId("IO");
		model.addAttribute("q1",
				surveyMng.findQuest("surveyId", surveyId, "sort"));
		sql = "from SurveyAnswer info where surveyId='" + surveyId + "'";
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='IO'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		model.addAttribute("a1", surveyAnswerMng.list(sql));

		surveyId = getSurveyId("CU");
		model.addAttribute("q2",
				surveyMng.findQuest("surveyId", surveyId, "sort"));
		sql = "from SurveyAnswer info where surveyId='" + surveyId + "'";
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='CU'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		model.addAttribute("a2", surveyAnswerMng.list(sql));

		surveyId = getSurveyId("LH");
		model.addAttribute("q3",
				surveyMng.findQuest("surveyId", surveyId, "sort"));
		sql = "from SurveyAnswer info where surveyId='" + surveyId + "'";
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='LH'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		model.addAttribute("a3", surveyAnswerMng.list(sql));

		surveyId = getSurveyId("CP");
		model.addAttribute("q4",
				surveyMng.findQuest("surveyId", surveyId, "sort"));
		sql = "from SurveyAnswer info where surveyId='" + surveyId + "'";
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='CP'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		model.addAttribute("a4", surveyAnswerMng.list(sql));

		surveyId = getSurveyId("GS");
		model.addAttribute("q5",
				surveyMng.findQuest("surveyId", surveyId, "sort"));
		sql = "from SurveyAnswer info where surveyId='" + surveyId + "'";
		sql += " and  answer_user='" + uid + "'";
		sql += " and p0='GS'";
		sql += " and p1='" + entpBexhiInfo.getId() + "'";
		model.addAttribute("a5", surveyAnswerMng.list(sql));
	}

	@ModelAttribute("surveyInfo")
	public SurveyInfo getSurveyInfo(
			@RequestParam(value = "sid", required = true, defaultValue = "") String code) {
		SurveyInfo surveyInfo = surveyMng.get(code);
		if (surveyInfo == null)
			surveyInfo = new SurveyInfo();
		return surveyInfo;
	}

	@ModelAttribute("entpBexhi")
	public EntpBexhi getEntpSexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpBexhi entpBexhi = entpBexhiMng.get(id);
		if (null == entpBexhi) {
			entpBexhi = new EntpBexhi();
		}
		return entpBexhi;
	}

	private void showView(EntpBexhi entpBexhi, ModelMap model) {
		model.addAttribute("info", entpBexhi);
		String zb = "";
		String qt = "";
		String cb = "";
		String zc = "";
		String xb = "";
		String exhiType = "";
		String zldd = "";

		if (entpBexhi.getId() == null) {
			// 第一主办方
			model.addAttribute("entpName", getEntpUser().getMc());
			model.addAttribute("projectId", Identities.uuid());
		} else {
			// 第一主办方
			model.addAttribute("entpName", entpMng.get(entpBexhi.getUid())
					.getMc());
			model.addAttribute("projectId", entpBexhi.getId());
			zb = this.getEntpRelaction(entpBexhi.getId(), "ZB");// 主办单位
			qt = this.getEntpRelaction(entpBexhi.getId(), "QT");// 其他单位
			cb = this.getEntpRelaction(entpBexhi.getId(), "CB");// 承办单位
			zc = this.getEntpRelaction(entpBexhi.getId(), "ZC");// 支持单位
			xb = this.getEntpRelaction(entpBexhi.getId(), "XB");// 协办单位

			exhiType = this.getEntpRelaction(entpBexhi.getId(), "exhiType");// 展会类型
			zldd = this.getEntpRelaction(entpBexhi.getId(), "zldd");// 展会场馆

		}
		model.addAttribute("zb", zb);
		model.addAttribute("qt", qt);
		model.addAttribute("cb", cb);
		model.addAttribute("zc", zc);
		model.addAttribute("xb", xb);
		model.addAttribute("exhiType", exhiType);
		model.addAttribute("zldd", zldd);
	}

	@Autowired
	@Qualifier("entpBexhiMng")
	private EntpBexhiMng entpBexhiMng;

	private String getUrl(String id) {
		String s = "";
		String surveyId = "";
		surveyId = getSurveyId("c2e");

		// 企业评估场馆
		s = "<a href='" + PATH_SURVEY_DETAIL.substring(1) + ".jspx?sid="
				+ surveyId + "&type=c2e&infoId=" + id + "'>【对场馆评测】</a>";
		s += "  <a href='asse-detail.jspx?code=B&infoId=" + id + "'>【展前预估】</a>";
		s += "  <a href='asse-detail.jspx?code=A&infoId=" + id + "'>【展后自评】</a>";

		s += "  <a href='" + PATH_SURVEY_DETAIL.substring(1) + ".jspx?sid="
				+ surveyId + "&type=e2c&infoId=" + id + "'>【对企业评测】</a>";

		return s;
	}

	@Autowired
	@Qualifier("surveyMng")
	private SurveyMng surveyMng;

	@Autowired
	@Qualifier("surveyAnswerMng")
	private SurveyAnswerMng surveyAnswerMng;
}
